const app = getApp();
const https = require('../../utils/https.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
      login_status = wx.getStorageSync('login_status');
    if (!login_status || login_status <= 0) {
      wx.showLoading({
        title: '请先进行登陆',
      })
      setTimeout(() => {
        wx.hideLoading({
          success: (res) => {
            wx.navigateTo({
              url: '/pages/grant/grant',
            })
          },
        })
      }, 1500);
    } else {
      that.getList();
    }
  },
  /**
   * @xk 获取收货地址列表
   */
  getList: function () {
    var that = this,
      user_id = wx.getStorageSync('user_id');
    https.post('getAddressList', {
      user_id: user_id
    }, false, '正在疯狂加载').then(res => {
      that.setData({
        address_list: res
      })
    })
  },
  /**
   *  @xk 跳转到地址添加
   * @param res 
   */
  jumpAdd: function () {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  del: function (obj) {
    var address_id = obj.currentTarget.dataset.address_id; //地址DI
    wx.showModal({
      title: '删除确认',
      content: '您确定要删除这个地址吗？',
      success: function (sm) {
        if (sm.confirm) {
          https.post('delAddress', {
            address_id: address_id
          }, false, '正在删除').then(res => {
            if (res) {
              this.getList();
            }
          })
        } else if (sm.cancel) {}
      }
    })

  },
  /**
   * @xk 编辑地址
   * @param {} obj 
   */
  edit: function (obj) {
    var address_id = obj.currentTarget.dataset.address_id;
    wx.navigateTo({
      url: '/pages/address/address?address_id=' + address_id,
    })
  },
  chooseAddress: function (obj) {
    var that = this,
      address_id = obj.currentTarget.dataset.address_id,
      cart_ids = app.globalData.cart_id;
    if (cart_ids != '' && cart_ids != null) {
      app.globalData.address_id = address_id;
      wx.navigateBack({
        delta: 0,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


})